package com.example.demo.security.config;

import com.example.demo.jwt.JwtTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class mySecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    JwtTokenFilter jwtTokenFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
//                formLogin()
//                .loginPage("/login.html")
//                .loginProcessingUrl("/login")
                // 成功，失败均返回json信息，适用于前后端分离项目
//                .successHandler(new MyAuthenticationSuccessHandler())
//                .failureHandler(new MyAuthenticationFailHandler());
                    // 直接跳转页面
//                .successForwardUrl("/test");

        http.authorizeRequests()
                .antMatchers("/test").permitAll()
                .antMatchers("/index.html").hasAnyAuthority("admin","normal")
                .antMatchers("/index.html").hasAnyRole("abc")
                .anyRequest().authenticated();

        http.csrf().disable();
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }


}
